package com.dcivision.framework.taglib.channel;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.PageContext;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.SystemParameterConstant;
import com.dcivision.framework.SystemParameterFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.workflow.bean.WorkflowProgress;

public abstract class AjaxStandardTrackingFormatter extends AbstractAjaxChannelFormatter {
  
  //the limit length of tracking InBox
  private static final int INBOX_TACKING_LIMIT_LENGTH = 15;
  //-------should init---------------------
  protected AjaxTagFactory  ajaxFactory = null;
  //-------scope:for{}
  protected WorkflowProgress workflowProgress = null;
  public AjaxStandardTrackingFormatter(){

  }
  public AjaxStandardTrackingFormatter(PageContext pageContext,int pageSize,List channelList){
    this.pageContext = pageContext;
    this.pageSize = pageSize;
    this.channelList = channelList;
    init();
  }

  public void init(){
    ajaxFactory = (AjaxTagFactory)pageContext.getRequest().getAttribute(AjaxConstant.AJAXTAGFACTORY);
    this.columnCount = 6;
    this.pattern="common.channel.task";
  }
  public String[] getContent() throws ApplicationException {
    StringBuffer bstr = new StringBuffer();
    int i=0;
    for(Iterator it=channelList.iterator();it.hasNext();){
      i++;
      workflowProgress = (WorkflowProgress)it.next();
      bstr.append("<tr>");
      if(i<pageSize&&i<channelList.size()){
      bstr.append(getPriority());
      bstr.append(getStatusFlag());
      bstr.append(getTrackID());
      bstr.append(getStepInfo());
      bstr.append(getReference());
      bstr.append(getDueDate());
      bstr.append(getWorkflowTaker());
      }else{
        bstr.append(addStyle(getPriority()));
        bstr.append(addStyle(getStatusFlag()));
        bstr.append(addStyle(getTrackID()));
        bstr.append(addStyle(getStepInfo()));
        bstr.append(addStyle(getReference()));
        bstr.append(addStyle(getDueDate()));
        bstr.append(addStyle(getWorkflowTaker()));
      }
      bstr.append("</tr>");
    }
    //bstr.append(this.getPageInfoContent(columnCount));
    //return bstr.toString();
    return this.parsePageInfo(bstr);
  }
  /**
   * @return string
   */
  private String addStyle(String oldStr) {
    return oldStr.replaceFirst("<td", "<td style=\" border-bottom:none \" ");
  }

  protected abstract String getPriority() throws ApplicationException;
  protected abstract String getStatusFlag() throws ApplicationException;
  protected abstract String getReference() throws ApplicationException;
  protected abstract String getTrackID() throws ApplicationException;
  protected abstract String getStepInfo() throws ApplicationException;
  protected abstract String getDueDate() throws ApplicationException;
  protected abstract String getWorkflowTaker() throws ApplicationException;

  protected String getPriority(StringBuffer bstr) throws ApplicationException{
    AjaxWorkflowPriorityTag priority = (AjaxWorkflowPriorityTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.workflowPriorityTag);
    priority.setWorkflowProgress(workflowProgress);
    bstr.append(priority.getAjaxContext());
    ajaxFactory.releaseTag(AjaxConstant.workflowPriorityTag, priority);
    return bstr.toString();
  }  
  protected String getTrackID(StringBuffer bstr) throws ApplicationException {
    bstr.append("<a ");
    bstr.append("href=\"#\" ");
    bstr.append("class=\"stepname\" ");
    bstr.append("onClick=\"return OpViewTrack(\'" +workflowProgress.getTrackID() + "\', \'" +
        workflowProgress.getID() + "\', \'" + workflowProgress.getWorkflowRecordID() + "\')\" ");  
    bstr.append (">");
    bstr.append(TextUtility.formatNumberWithZeroPrefix(SystemParameterFactory.getSystemParameterInteger(
        SystemParameterConstant.WORKLFLOW_TRACK_ID_FORMAT_LENGTH).intValue(), (workflowProgress).getTrackID()));
    bstr.append ( "</a>" );
    return bstr.toString();
  }
  protected String getStatusFlag(StringBuffer bstr) throws ApplicationException{
    AjaxStatusFlagTag statusFlag = (AjaxStatusFlagTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.workflowStatusFlagTag);
    statusFlag.setWorkflowProgress(workflowProgress);
    statusFlag.setInProgressIcon("IconRedFlag.gif");
    statusFlag.setCompletedIcon("IconGreyFlag.gif");
    bstr.append(statusFlag.getAjaxContext());
    ajaxFactory.releaseTag(AjaxConstant.workflowStatusFlagTag, statusFlag);
    return bstr.toString();
  }
  protected String getReference(StringBuffer bstr) throws ApplicationException{
    AjaxRelateObjectInfoTag relateObjectInfo = (AjaxRelateObjectInfoTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.relateObjectInfoTag);
    relateObjectInfo.setTrackID(workflowProgress.getTrackID());
    relateObjectInfo.setWorkflowProgress(workflowProgress);
    relateObjectInfo.setRelatedItem(false);
    relateObjectInfo.setLimitLength(INBOX_TACKING_LIMIT_LENGTH);
    relateObjectInfo.setStyleClass("stepname");
    relateObjectInfo.setHomePage(true);
    relateObjectInfo.setTracking(true); 
    bstr.append(relateObjectInfo.getAjaxContent().replaceAll("onClick\\s*=\\s*\"", "  style=\"text-decoration: none; cursor:text; \"   onClick=\"return (false);"));
    ajaxFactory.releaseTag(AjaxConstant.relateObjectInfoTag, relateObjectInfo);
    return bstr.toString();
  }
  protected String getStepInfo(StringBuffer bstr) throws ApplicationException{
    if(!Utility.isEmpty(workflowProgress.getStageName())){
      bstr.append(workflowProgress.getStageName());
    }else{
      AjaxWorkflowStepInfoTag workflowStepInfo = (AjaxWorkflowStepInfoTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.workflowStepInfoTag);
      workflowStepInfo.setWorkflowProgress(workflowProgress);
      workflowStepInfo.setFunctionCode(SystemFunctionConstant.WORKFLOW_TRACK);
      workflowStepInfo.setDelegateUserRecordID("");
      workflowStepInfo.setNavMode(GlobalConstant.NAV_MODE_EDIT);
      workflowStepInfo.setStyleClass("stepname");
      bstr.append(workflowStepInfo.getAjaxContent());
      ajaxFactory.releaseTag(AjaxConstant.workflowStepInfoTag, workflowStepInfo);
    }
    return bstr.toString();
  }
  protected String getDueDate(StringBuffer bstr) throws ApplicationException{
    AjaxWorkflowDudateTag workflowDudate = (AjaxWorkflowDudateTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.workflowDudateTag);
    workflowDudate.setStatus(workflowProgress.getStatus());
    workflowDudate.setDueDate(workflowProgress.getDueDate());
    bstr.append(workflowDudate.getAjaxContent());
    ajaxFactory.releaseTag(AjaxConstant.workflowDudateTag, workflowDudate);
    return bstr.toString();
  }
  protected String getWorkflowTaker(StringBuffer bstr) throws ApplicationException{
    AjaxWorkflowActionTakerTag workflowTaker = (AjaxWorkflowActionTakerTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.workflowActionTakerTag);
    workflowTaker.setWorkflowProgress(workflowProgress);
    workflowTaker.setAssignedTo(GlobalConstant.TRUE);
    bstr.append(workflowTaker.getAjaxContent());
    ajaxFactory.releaseTag(AjaxConstant.workflowActionTakerTag, workflowTaker);
    return bstr.toString();
  }
}
